package com.settlement.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.model.entity.SysAccessory;
import com.settlement.system.model.form.AccessoryForm;
import com.settlement.system.model.query.AccessoryPageQuery;
import com.settlement.system.service.SysAccessoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 辅助项设置 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-09-07
 */
@Tag(name = "辅助项表")
@RestController
@RequestMapping("/api/v1/accessory")
@RequiredArgsConstructor
public class SysAccessoryController {

    private final SysAccessoryService sysAccessoryService;

    @Operation(summary = "辅助项表分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<SysAccessory> getPage(@ParameterObject AccessoryPageQuery queryParams) {
        Page<SysAccessory> result = sysAccessoryService.getAccessoryPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "保存辅助项表", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:accessory:save')")
    @PreventDuplicateSubmit
    public Result<?> saveAccessory(@RequestBody @Valid AccessoryForm accessoryForm) {
        boolean result = sysAccessoryService.saveAccessory(accessoryForm);
        return Result.judge(result);
    }

    @Operation(summary = "辅助项表单数据", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{accessoryId}/form")
    public Result<SysAccessory> getAccessoryForm(@Parameter(description = "科目表ID") @PathVariable Long accessoryId) {
        SysAccessory sysAccessory = sysAccessoryService.getAccessoryFormData(accessoryId);
        return Result.success(sysAccessory);
    }

    @Operation(summary = "通过末级科目查找辅助项", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/findAll")
    public Result<?> findAll() {
        List<SysAccessory> sysAccessoryList = sysAccessoryService.findAll();
        return Result.success(sysAccessoryList);
    }

    @Operation(summary = "删除辅助项表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:accessory:delete')")
    public Result<?> deleteAccessory(@Parameter(description = "辅助项表ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = sysAccessoryService.deleteAccessory(ids);
        return Result.judge(result);
    }

}
